package com.sf.pathmemorymethod;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by YSir on 2018/7/7.
 */


public class NumberMemoryActivity extends Activity {

    private EditText num_length;
    private Button btn_set_length;
    private static TextView num_area;
    private EditText num_answer;
    private Button btn_compare;
    private Button btn_show_or_hide;
    private static RadioButton open_show_dif;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_num_memory);

        //绑定各组件
        num_length = (EditText) findViewById(R.id.edit_num_length);
        btn_set_length = (Button) findViewById(R.id.btn_set_length);
        num_area = (TextView) findViewById(R.id.num_area);
        num_answer = (EditText) findViewById(R.id.edit_num_answer);
        btn_compare = (Button) findViewById(R.id.btn_compare);
        btn_show_or_hide = (Button) findViewById(R.id.btn_show_or_hide);
        open_show_dif = (RadioButton) findViewById(R.id.open_show_dif);


        //设置数字长度
        btn_set_length.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String str_length = num_length.getText().toString();
                if (!str_length.equals("")) {
                    int length = Integer.parseInt(num_length.getText().toString());
                    int num_array[] = new int[length];
                    for (int idx=0; idx<length; idx++){
                        num_array[idx] = (int) (Math.random()*10);
                    }
                    String new_num = toStringMethod(num_array);
                    num_area.setText(new_num+"");

                }

            }
        });

        //提交答案
        btn_compare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String question = num_area.getText().toString();
                String answer = num_answer.getText().toString();

                if (!question.equals("")){
                    switch (compareNum(question,answer)){
                        case 1:
                            Toast.makeText(NumberMemoryActivity.this,"恭喜，全对噢",Toast.LENGTH_SHORT).show();
                            break;
                        case 2:
                            Toast.makeText(NumberMemoryActivity.this,"有的数字不对噢",Toast.LENGTH_SHORT).show();
                            break;
                        case 3:
                            Toast.makeText(NumberMemoryActivity.this,"请检查一下数字长度对不对噢",Toast.LENGTH_SHORT).show();
                            break;
                        default:
                            break;
                    }
                }



            }
        });

        //隐藏/显示数字
        btn_show_or_hide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int isShow = num_area.getVisibility();
                if (isShow == View.VISIBLE){
                    num_area.setVisibility(View.GONE);
                }else {
                    num_area.setVisibility(View.VISIBLE);
                }
            }
        });

    }

    //数组转String
    private static String toStringMethod(int[] arr){
        //自定义一个缓冲区
        StringBuilder sb = new StringBuilder();
        for (int idx=0; idx<arr.length; idx++){
            sb.append(arr[idx]);
        }
        String new_num = sb.toString();
        return new_num;
    }


    //答案判断
    private static int compareNum(String question,String answer){
        //错误标记:1.全对；2.有错；3.长度不对
        int label = 1;

        //问题转成数组
        char[] charQuestionArray = question.toCharArray();
        int[] numQuestion = new int [charQuestionArray.length];
        for(int idx=0; idx<numQuestion.length; idx++){
            numQuestion[idx] = Integer.parseInt(String.valueOf(charQuestionArray[idx]));
        }

        //答案转成数组
        char[] charAnswerArray = answer.toCharArray();
        int[] numAnswer = new int [charAnswerArray.length];
        for(int idx=0; idx<numAnswer.length; idx++){
            numAnswer[idx] = Integer.parseInt(String.valueOf(charAnswerArray[idx]));
        }

        //验证
        //如果纠错模式开启
        if (open_show_dif.isChecked()){
            if (numQuestion.length == numAnswer.length){
                SpannableStringBuilder builder = new SpannableStringBuilder(question);
                for (int idx = 0; idx<numQuestion.length; idx++){
                    if (numQuestion[idx] != numAnswer[idx]){
                        builder.setSpan(new ForegroundColorSpan(Color.RED),idx,idx+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        label = 2;
                    }
                }
                num_area.setText(builder);
            }else {
                label = 3;
            }

        }else {
            //纠错模式关闭，比较问题与答案,直接判断有没有错
            if (numQuestion.length == numAnswer.length){
                for (int idx=0; idx<numQuestion.length; idx++){
                    if (numQuestion[idx] != numAnswer[idx]){
                        label = 2;
                    }
                }
            }else {
                label = 3;
            }

        }

        return label;
    }

}
